<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px" @submit.prevent>
      <el-form-item label="容器编号" prop="containerId">
        <el-input
          v-model.trim="queryParams.containerId"
          placeholder="请输入容器料箱编号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="物料码" prop="materialCode">
        <el-input
          v-model.trim="queryParams.materialCode"
          placeholder="请输入物料码"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item label="储位编码" prop="storageId">
        <el-input
          v-model.trim="queryParams.storageId"
          placeholder="请输入储位编码"
          clearable
          @keyup.enter="handleQuery"
          style="width: 175px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:body:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:body:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:body:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:body:export']"
        >导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downloadTemplate"
        >下载导入模板</el-button>
      </el-col>
      <el-col :span="1.5">
        <MyUpload
          buttonName="导入模板"
          url="/api/inventory/check/body/import"
          @success="handleUploadSuccess"
          @warning="handleUploadWarning"
          />
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <TableScrollContainer :topHeight="220">
      <template #default="{ tableHeight }">    
        <el-table v-loading="loading" :data="bodyList" @selection-change="handleSelectionChange" :height="tableHeight" border>
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="自增主键" align="center" prop="id" /> -->
          <el-table-column label="盘点任务单号" align="center" prop="inventoryCheckCode" />
          <!-- <el-table-column label="料箱编号" align="center" prop="containerId" /> -->
          <el-table-column label="物料码" align="center" prop="materialCode" />
          <el-table-column label="盘点数量（空为固定盘）" align="center" prop="bathNo" />
          <el-table-column label="MES账存数量" align="center" prop="mesStockQty" />                        
          <!-- <el-table-column label="储位编码" align="center" prop="storageId" /> -->
          <el-table-column label="盘点状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="check_detail_status" :value="scope.row.status"></dict-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
            <template #default="scope">
              <el-button type="success" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:body:edit']">修改</el-button>
              <el-button type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:body:remove']">删除</el-button>
            </template>
          </el-table-column> -->
        </el-table>
    
        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[10, 30, 50, 100]"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
    </TableScrollContainer>    

    <!-- 添加或修改盘点单体对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" :close-on-click-modal="false">
      <el-form ref="bodyRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="盘点任务单号" prop="inventoryCheckCode">
          <el-input v-model="form.inventoryCheckCode" placeholder="请输入盘点任务单号" />
        </el-form-item>
        <el-form-item label="料箱编号" prop="containerId">
          <el-input v-model="form.containerId" placeholder="请输入料箱编号" />
        </el-form-item>
        <el-form-item label="物料码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料码" />
        </el-form-item>
        <el-form-item label="储位编码" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入储位编码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Body">
import { listBody, getBody, delBody, addBody, updateBody } from "@/api/work/checkOrderDetail"
import TableScrollContainer from '@/components/Pagetable/TableScrollContainer.vue'

const { proxy } = getCurrentInstance()
const { check_detail_status } = proxy.useDict('check_detail_status')
const route = useRoute()

const bodyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    containerId: null,
    materialCode: null,
    storageId: null,
    inventoryCheckCode: route.query.inventoryCheckCode || null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询盘点单体列表 */
function getList() {
  loading.value = true
  listBody(queryParams.value).then(res => {
    if (res.errCode === '1') {
      bodyList.value = res.data.page.records
      total.value = res.data.page.total
      loading.value = false
    }
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inventoryCheckCode: null,
    containerId: null,
    materialCode: null,
    storageId: null,
    status: null,
    finishTime: null
  }
  proxy.resetForm("bodyRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加盘点单体"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBody(_id).then(res => {
    // form.value = res.data.inventory/check/body
    open.value = true
    title.value = "修改盘点单体"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bodyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBody(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      } else {
        addBody(form.value).then(res => {
          if (res.errCode === '1') {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          } else {
            proxy.$modal.msgError(res.msg)
          }
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除盘点单体编号为"' + _ids + '"的数据项？').then(function() {
    return delBody(_ids)
  }).then(res => {
    if (res.errCode === '1') {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    } else {
      proxy.$modal.msgError(res.msg)
    }
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('api/inventory/check/body/export', {
    ...queryParams.value
  }, `body数据_${new Date().getTime()}.xlsx`)
}

getList()

// 下载模板
const downloadTemplate = async() => {
  proxy.download('/api/inventory/check/body/downloadTemplate', {}, `body模板_${new Date().getTime()}.xlsx`)
}
// 上传成功后的回调
const handleUploadSuccess = (res) => {
  getList()
}
// 部分成功回调
const handleUploadWarning = (res) => {
  getList()
}
</script>
